/**
 * 
 */
package com.zhaoyan.cms.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageInfo;
import com.zhaoyan.cms.common.CmsResultMsg;
import com.zhaoyan.cms.constant.CmsConstant;
import com.zhaoyan.cms.pojo.Article;
import com.zhaoyan.cms.pojo.Link;
import com.zhaoyan.cms.pojo.User;
import com.zhaoyan.cms.pojo.vo.ArticleVo;
import com.zhaoyan.cms.service.ArticleService;
import com.zhaoyan.cms.service.LinkService;
import com.zhaoyan.cms.service.UserService;

/**
 * @author zhaoyan
 *
 * 2020年1月23日
 */
@Controller
@RequestMapping("admin")
public class AdminController {

	
	@Autowired
	private ArticleService  articleService;
	
	
	@Autowired
	private UserService userService;
	
	
	@Autowired
	private LinkService linkService;
	
	
	/**
	 * 去到后台管理页面
	 * @return
	 */
	@RequestMapping(value = "toShowAll",method =RequestMethod.GET)
	private String toShowAll() {
		
		return "superM/show";
	}
	
	/**
	 * 查询文章
	 * @param vo
	 * @param pageNum
	 * @param m
	 * @return
	 */
	@RequestMapping("showArticle")
	public String showArticle(ArticleVo vo,@RequestParam(defaultValue = "1")Integer pageNum,Model m) {
		
		
		System.err.println(pageNum);
		PageInfo<Article> articlePage = articleService.findArticleList(vo,pageNum);
		m.addAttribute("articlePage",articlePage);
		
		return "superM/article";
	}
	/**
	 * 查询用户
	 * @param m
	 * @param pageNum
	 * @return
	 */
	@RequestMapping("showUser")
	public String showUser( Model m , @RequestParam(defaultValue = "1")Integer pageNum ) {
		
		System.err.println(pageNum);
		
		PageInfo<User> userPage = userService.findUserList(pageNum);
		m.addAttribute("userPage", userPage);
		
		return "superM/users";
	}
	
	/**
	 *	 获取到文章的
	 * @param id
	 * @return
	 */
	@RequestMapping("getArticle")
	@ResponseBody
	public CmsResultMsg<Article> checkArticle(Integer id) {
		
		Article article = articleService.findArticle(id);
		
		if( article == null) {
			return new CmsResultMsg<Article>(1,CmsConstant.FAILED,null);
		}
		
		return new CmsResultMsg<Article>(0,CmsConstant.SUCCEED,article);
		
	}
	
	/**
	 * 	封禁文章
	 * @param id
	 * @param statusId
	 * @return
	 */
	@RequestMapping("setStatus")
	@ResponseBody
	public CmsResultMsg<String> setStatus(Integer id,Integer statusId){
		
		Integer num = articleService.setStatus(id,statusId);
		
		if(num > 0) {
			return new CmsResultMsg<String>(0,CmsConstant.SUCCEED,"封禁成功");
		}
		
		return new CmsResultMsg<String>(1,CmsConstant.FAILED,"封禁失败");
	}
	/**
	 * 	设置文章的热门
	 * @param id
	 * @param statusId
	 * @return
	 */
	@RequestMapping("setHot")
	@ResponseBody
	public CmsResultMsg<String> setHot(Integer id,Integer statusId){
		
		Integer num = articleService.setHot(id,statusId);
		
		
		if(num > 0) {
			return new CmsResultMsg<String>(0,CmsConstant.SUCCEED,"设置热门成功");
		}
		
		return new CmsResultMsg<String>(1,CmsConstant.FAILED,"设置热门失败失败");
	}
	
	@RequestMapping("setUser")
	@ResponseBody
	public CmsResultMsg<String> setUser(Integer userId,Integer lockedId){
		
		
		Integer num = articleService.setUser(userId,lockedId);
		
		if(num > 0) {
			return new CmsResultMsg<String>(0,CmsConstant.SUCCEED,"封禁成功");
		}
		
		return new CmsResultMsg<String>(1,CmsConstant.FAILED,"封禁失败");
	}
	
	
	
	//友情链接
	/**
	 * 	友情链接的管理展示页面
	 * @param m
	 * @param pageNum
	 * @return
	 */
	@RequestMapping("showLink")
	public String showLink(Model m ,@RequestParam(defaultValue = "1") Integer pageNum ) {
		
		PageInfo<Link> linkPage = linkService.findLinkList(pageNum);
		
		m.addAttribute("linkPage",linkPage);
		
		return "superM/link";
	}
	/**
	 * 去添加页面
	 * @return
	 */
	@RequestMapping("toAddLink")
	public String toAddLink() {
		
		return "superM/link/addLink";
	}
	
	/**
	 * 添加友情链接
	 * @param link
	 * @return
	 */
	@RequestMapping("addLink")
	@ResponseBody
	public CmsResultMsg<String> addLink(Link link){
		
		Integer num = linkService.addLink(link);
		
		if(num > 0) {
			
			return new CmsResultMsg<String>(0,CmsConstant.SUCCEED,"链接添加成功");
		}
		
		
		return new CmsResultMsg<String>(6,CmsConstant.FAILED,"链接添加失败");
	}
	/**
	 * 	删除链接
	 * @param id
	 * @return
	 */
	@RequestMapping("delete")
	@ResponseBody
	public CmsResultMsg<String> delLink(Integer id){
		
		Integer num = linkService.delLink(id);
		
		if(num > 0) {
			
			return new CmsResultMsg<String>(0,CmsConstant.SUCCEED,"删除成功");
		}
		
		
		return new CmsResultMsg<String>(6,CmsConstant.FAILED,"删除失败");
		
	}
	
	/**
	 * 	去修改界面
	 * @param id
	 * @param m
	 * @return
	 */
	@RequestMapping("toUpdateLink")
	public String findLinkById(Integer id,Model m) {
		
		Link link = linkService.findLinkById(id); 
		
		m.addAttribute("link", link);
		
		return "superM/link/updateLink";
		
	}
	
	@RequestMapping("updateLink")
	@ResponseBody
	public CmsResultMsg<String> updateLink(Link link){
		
		Integer num = linkService.updateLink(link);
		
		if(num > 0) {
			
			return new CmsResultMsg<String>(0,CmsConstant.SUCCEED,"链接添加成功");
		}
		
		
		return new CmsResultMsg<String>(6,CmsConstant.FAILED,"链接添加失败");
		
		
		
	}
	
	
}
